package org.duanyu.shopping_order_customer_api.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.ibatis.annotations.Delete;
import org.duanyu.shopping_common.pojo.Address;
import org.duanyu.shopping_common.pojo.Area;
import org.duanyu.shopping_common.pojo.City;
import org.duanyu.shopping_common.pojo.Province;
import org.duanyu.shopping_common.result.BaseResult;
import org.duanyu.shopping_common.service.AddressService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址
 */
@RestController
@RequestMapping("/user/address")
public class AddressController {

    @DubboReference
    private AddressService addressService;

    /**
     *查询所有省份
     * @return 执行结果
     */
    @GetMapping("/findAllProvince")
    public BaseResult<List<Province>> findAllProvince(){
        List<Province> allProvince = addressService.findAllProvince();
        return BaseResult.success(allProvince);
    }

    /**
     * 查询所有城市
     * @param provinceId 省份id
     * @return 执行结果
     */
    @GetMapping("/findCityByProvince")
    public BaseResult<List<City>> findCityByProvince(Long provinceId){
        List<City> allCity = addressService.findAllCityByProvince(provinceId);
        return BaseResult.success(allCity);
    }

    /**
     * 查询所有地区
     * @param cityId 城市id
     * @return 执行结果
     */
    @GetMapping("/findAreaByCity")
    public BaseResult<List<Area>> findAreaByCity(Long cityId){
        List<Area> findAllArea=addressService.findAllAreaByCity(cityId);
        return BaseResult.success(findAllArea);
    }

    /**
     * 添加地址
     * @param userId 用户id
     * @param address 地址对象
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestHeader Long userId,@RequestBody Address address){
        address.setUserId(userId);
        addressService.add(address);
        return BaseResult.success();
    }

    /**
     * 更新地址
     * @param userId 用户id
     * @param address 地址对象
     * @return 执行结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestHeader Long userId,@RequestBody Address address){
        address.setUserId(userId);
        addressService.update(address);
        return BaseResult.success();
    }

    /**
     * 根据地址id查询地址，只查一条数据
     * @param id 地址id
     * @return 执行结果
     */
    @GetMapping("/findById")
    public BaseResult<Address> findById(Long id){
        Address addressServiceById = addressService.findById(id);
        return BaseResult.success(addressServiceById);
    }

    /**
     * 删除地址
     * @param id 地址id
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long id){
        addressService.delete(id);
        return BaseResult.success();
    }

    /**
     * 根据用户id查询地址 查询的是一个集合
     * @param userId 用户id
     * @return 执行结果
     */
    @GetMapping("/findByUser")
    public BaseResult<List<Address>> findByUserId(@RequestHeader Long userId){
        List<Address> addresses = addressService.findByUser(userId);
        return BaseResult.success(addresses);
    }

}
